package main

import (
	"fmt"
	_ "io"
	"log"
	"math/big"
	"os"
	"strconv"

	"github.com/ethereum/go-ethereum/accounts/abi/bind"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/ethclient"
	"github.com/ethereum/go-ethereum/rpc"
)

func newAcc(pass string) {
	//
	cli, err := rpc.Dial("http://localhost:8545")
	if err != nil {
		log.Fatal("failed to connect to geth", err)
	}
	defer cli.Close()
	var account string
	//返回结果，方法名，参数
	err = cli.Call(&account, "personal_newAccount", pass)
	if err != nil {
		log.Fatal("falid to call geth", err)
	}
	fmt.Println("new acc:", account)

}

func main() {
	//newAcc("zzj")
	//getPerson(0)
	//addPerson("sdfsdfsdfsdf", 12)
	TsfInput()
}

func TsfInput() {
	fmt.Println("请输入接受转帐得地址")
	var addr string
	fmt.Scanf("%s", &addr)
	var token string
	var value int64
	fmt.Println("请输入接受转帐得金额")

	for {
		fmt.Scanf("%s", &token)
		number, err := strconv.Atoi(string(token))
		if err != nil {
			fmt.Println("请输入接受转帐得金额")
		} else {
			value = int64(number)
			break
		}
	}
	fmt.Println(addr, value)
	Transfer(addr, value)
}

func Transfer(addr string, value int64) {
	//1.dial
	cli, err := ethclient.Dial("http://localhost:8545")
	if err != nil {
		log.Fatal("failet to conn to geth")
	}
	//2.rukou  // 合约地址
	instance, err := NewCallabi(common.HexToAddress("0x918924f2d4a2021530d1c19f8226ea6156975c86"), cli)
	if err != nil {
		log.Fatal("failet to conn to NewCallabi")
	}

	//3设置签名 --
	//keystore lu jing  //账户地址
	file, err := os.Open("/home/work/data/keystore/UTC--2018-12-09T04-58-17.735482771Z--243639aff4f2e1a491daded8f9da6a6ca7910e8b")
	if err != nil {
		log.Fatal("failet to conn to Open")
	}
	//接受转张地址
	auth, err := bind.NewTransactor(file, "")
	if err != nil {
		log.Fatal("failet to conn to NewTransactor", err)
	}
	instance.Transfer(auth, common.HexToAddress(addr), big.NewInt(value))

}
